package Main;

import url.DBUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class InsertFrame extends JFrame {

    InsertFrame() {
        Font f = new Font("宋体", Font.PLAIN, 18);
        // 窗口大小
        setSize(300, 300);
        // 窗口可视化
        setVisible(true);
        // 窗口标题
        setTitle("场地管理系统");
        // 窗口初始化位置
        setLocationRelativeTo(null);
        // 窗口大小不可改变
        setResizable(false);
        // 窗口的关闭事件
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(null); // 使用绝对布局
        centerPanel.setBackground(Color.cyan);

        JLabel jp1 = new JLabel("编号:");
        jp1.setBounds(50, 35, 80, 25); // 设置组件的位置和大小
        jp1.setFont(f);
        centerPanel.add(jp1);

        JTextField jtf1 = new JTextField(5);
        jtf1.setBounds(100, 35, 150, 25);
        jtf1.setFont(new Font("宋体", Font.PLAIN, 16));
        centerPanel.add(jtf1);

        JLabel jp2 = new JLabel("名称:");
        jp2.setBounds(50, 75, 80, 25);
        jp2.setFont(f);
        centerPanel.add(jp2);

        JTextField jtf2 = new JTextField(10);
        jtf2.setBounds(100, 75, 150, 25);
        jtf2.setFont(new Font("宋体", Font.PLAIN, 16));
        centerPanel.add(jtf2);

        JLabel jp3 = new JLabel("容量:");
        jp3.setBounds(50, 115, 80, 25);
        jp3.setFont(f);
        centerPanel.add(jp3);

        JTextField jtf3 = new JTextField(10);
        jtf3.setBounds(100, 115, 150, 25);
        jtf3.setFont(new Font("宋体", Font.PLAIN, 16));
        centerPanel.add(jtf3);


        JLabel jp4 = new JLabel("位置:");
        jp4.setBounds(50, 150, 80, 25);
        jp4.setFont(f);
        centerPanel.add(jp4);

        JTextField jtf4 = new JTextField(10);
        jtf4.setBounds(100, 150, 150, 25);
        jtf4.setFont(new Font("宋体", Font.PLAIN, 16));
        centerPanel.add(jtf4);


        JButton jbt1 = new JButton("确定");
        jbt1.setBounds(110, 200, 80, 25);
        jbt1.setFont(new Font("宋体", Font.PLAIN, 20));
        centerPanel.add(jbt1);

        add(centerPanel);

        jbt1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String a,b,c,d;
                a=jtf1.getText();
                b=jtf2.getText();
                c=jtf3.getText();
                d=jtf4.getText();
                DBUtil.initst();
                String sql = "insert into venue values(?,?,?,?,?)";
                try {
                    DBUtil.ps = DBUtil.con.prepareStatement(sql);
                    DBUtil.ps.setString(1,a);
                    DBUtil.ps.setString(2,b);
                    DBUtil.ps.setInt(3,Integer.parseInt(c));
                    DBUtil.ps.setInt(4,0);
                    DBUtil.ps.setString(5,d);
                    DBUtil.ps.executeUpdate();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
                dispose();
            }
        });
    }

}
